package com.dlc.shop.multishop.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dlc.shop.bean.enums.SendType;
import com.dlc.shop.bean.model.NotifyTemplateRemind;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.common.util.PageParam;
import com.dlc.shop.security.multishop.util.SecurityUtils;
import com.dlc.shop.service.NotifyTemplateRemindService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;


/**
 * 消息提醒设置表
 *
 * @author LGH
 * @date 2022-10-26 15:51:49
 */
@RestController
@RequestMapping("/multishop/notifyTemplateRemind")
@Tag(name = "消息提醒设置表")
@AllArgsConstructor
public class NotifyTemplateRemindController {

    private final NotifyTemplateRemindService notifyTemplateRemindService;

    @GetMapping("/page")
    @Operation(summary = "获取消息提醒设置表列表" , description = "分页获取消息提醒设置表列表")
    @PreAuthorize("@pms.hasPermission('shop:notifyTemplateRemind:page')")
    public ServerResponseEntity<IPage<NotifyTemplateRemind>> getNotifyTemplateRemindPage(PageParam<NotifyTemplateRemind> page, NotifyTemplateRemind notifyTemplateRemind) {
        notifyTemplateRemind.setShopId(SecurityUtils.getShopUser().getShopId());
        Page<NotifyTemplateRemind> notifyTemplateRemindPage = notifyTemplateRemindService.pageNotifyTemplateRemind(page, notifyTemplateRemind);
        //没有消息提醒设置记录，默认开启提醒
        notifyTemplateRemindPage.getRecords().forEach(entity -> {
            if(Objects.isNull(entity.getIsRemind())){
                entity.setIsRemind(1);
            }
            entity.setMenu(SendType.instance(entity.getSendType()).getMenu());
            entity.setNodeName(SendType.instance(entity.getSendType()).getNodeName());
        });
        return ServerResponseEntity.success(notifyTemplateRemindPage);
    }

    @GetMapping("/info/{notifyTemplateRemindId}")
    @Operation(summary = "获取消息提醒设置表" , description = "根据notifyTemplateRemindId获取消息提醒设置表")
    @Parameter(name = "notifyTemplateRemindId", description = "消息提醒设置表" , required = true)
    @PreAuthorize("@pms.hasPermission('shop:notifyTemplateRemind:info')")
    public ServerResponseEntity<NotifyTemplateRemind> getById(@PathVariable("notifyTemplateRemindId") Long notifyTemplateRemindId) {
        return ServerResponseEntity.success(notifyTemplateRemindService.getById(notifyTemplateRemindId));
    }

    @PostMapping
    @Operation(summary = "保存消息提醒设置表" , description = "保存消息提醒设置表")
    @PreAuthorize("@pms.hasPermission('shop:notifyTemplateRemind:save')")
    public ServerResponseEntity<Boolean> save(@RequestBody @Valid NotifyTemplateRemind notifyTemplateRemind) {
        return ServerResponseEntity.success(notifyTemplateRemindService.save(notifyTemplateRemind));
    }

    @PutMapping
    @Operation(summary = "更新消息提醒设置表" , description = "更新消息提醒设置表")
    @PreAuthorize("@pms.hasPermission('shop:notifyTemplateRemind:update')")
    public ServerResponseEntity<Boolean> updateById(@RequestBody @Valid NotifyTemplateRemind notifyTemplateRemind) {
        notifyTemplateRemind.setShopId(SecurityUtils.getShopUser().getShopId());
        //提醒设置记录为空，则保存记录
        if(Objects.isNull(notifyTemplateRemind.getNotifyTemplateRemindId())){
            notifyTemplateRemindService.save(notifyTemplateRemind);
        }else{
            notifyTemplateRemindService.updateById(notifyTemplateRemind);
        }
        return ServerResponseEntity.success();
    }

    @DeleteMapping("/{notifyTemplateRemindId}")
    @Operation(summary = "删除消息提醒设置表" , description = "根据消息提醒设置表id删除消息提醒设置表")
    @Parameter(name = "notifyTemplateRemindId", description = "消息提醒设置表" , required = true)
    @PreAuthorize("@pms.hasPermission('shop:notifyTemplateRemind:delete')")
    public ServerResponseEntity<Boolean> removeById(@PathVariable Long notifyTemplateRemindId) {
        return ServerResponseEntity.success(notifyTemplateRemindService.removeById(notifyTemplateRemindId));
    }
}
